﻿using System;
using System.Collections.Generic;
using System.Text;
using Loom;
using Loom.JoinPoints;
using System.Windows.Forms;
using IBuy.DataAccessLayer;
using IBuy.Entity;

namespace IBuy.AOP
{
    public class LogAspect :AspectAttribute
    {
        #region 记录系统日志
        [Call(Advice.After)]
        [Include("Open")]
        public void Open()
        {
            Log systemLog = new Log();
            systemLog.Degree = LogDegree.System;
            systemLog.Message = DateTime.Now.ToString() + "-连接数据库！";
            systemLog.SaveToSystemLog(systemLog);
        }

        [Call(Advice.AfterReturning)]
        [Include("BuyProducts")]
        public void Buy([JPRetVal] ref bool returnValue, int productId, int count)
        {
            if (returnValue)
            {
                Log log = new Log();
                log.Degree = LogDegree.User;
                log.Message = "用户A(agency a) 于"+DateTime.Now.ToString()+"时间购买了"+productId.ToString()+"*"+count.ToString();
                log.SaveToUserLog(log);
            }
        }

        [Call(Advice.AfterThrowing)]
        [Include("BuyProducts")]
        public T Buy<T>([JPException] Exception exceptione, int productId, int count)
        {
            Log log = new Log();
            log.Degree = LogDegree.User;
            log.Message = "用户A(agency a) 于" + DateTime.Now.ToString() + "时间购买了" + productId.ToString() + "*" + count.ToString() + "失败，由于" + exceptione.Message;
            log.SaveToUserLog(log);
            return default(T);
        }
        #endregion
    }
}
